package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.pawnshop.PawnshopConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class PawnshopRecycleStragety extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        PawnshopConfig config = PawnshopMgr.config;
        if (config != null) {
            ActivityInfo activityInfo = config.getActivityInfo();
            long recycleTime = activityInfo.getEndTime() * 1000 + 600 * 1000;
            int itemId = PawnshopMgr.config.staminaGoodsId;
            List<String> paramList = new ArrayList<>();
            paramList.add(String.valueOf(PawnshopMgr.config.scoreGoodsId));
            paramList.add(activityInfo.getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 0, recycleTime, paramList);
        }
    }

    @Override
    public boolean isActivityRunning() {
        PawnshopConfig config = PawnshopMgr.config;
        if (config != null) {
            ActivityInfo activityInfo = config.getActivityInfo();
            return ActivityMgr.activityInTime(activityInfo);
        }
        return false;
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.PawnshopActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        int itemId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, itemId);
        if (userBagItem != null && userBagItem.getNum() > 0) {
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(itemId);
            List<String> paramList = data.getParamList();
            int scoreGoodsId = Integer.parseInt(paramList.get(0));
            GoodsInfo scoreGoodsInfo = GoodsMgr.getGoodsById(scoreGoodsId);
            if (goodsInfo == null || scoreGoodsInfo == null) {
                log.error("remove  activity {} goods to score error,goods no found,goodsId:{}", activityType, itemId);
                return;
            }
            Property reward = new Property();
            long num =  userBagItem.getNum();
            if (removeItem(userBagItem)) {
                BigInteger addScore = goodsInfo.getParamList().get(1);
                addScore = addScore.multiply(BigInteger.valueOf(num));
                reward.addProperty(scoreGoodsId, addScore);

                String language = UserMgr.getUserInfo(userId).getLanguage();
                String scoreName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
                String scoreGoodsInfoName = ServerLanguageMgr.getContent(scoreGoodsInfo.getGoodsName(), language);
                Object[] paramArr = new Object[12];
                int i = 0;
                String activityName = ServerLanguageMgr.getContent(paramList.get(1), language);
                paramArr[i++] = activityName;
                paramArr[i++] = scoreName;
                paramArr[i++] = num;
                paramArr[i++] = scoreGoodsInfoName;
                String rewardStr = PropertyHelper.parsePropertyToString(reward);
                log.info("yongandang user {} rewardStr:{}", userId, rewardStr);
                if (!reward.isNothing()) {//有将道具转换
                    String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                            language), activityName);
                    String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                            language), paramArr);
                    MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
                }

            }
        }
    }
}
